package de.op.projectc.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import de.op.projectc.MainGame;
import de.op.projectc.stats.Options;

import javax.swing.*;

public class OptionScreen implements Screen {
    final MainGame game;
    private Stage stage;
    private Skin skin;

    private SelectBox dropdownControl;
    private SelectBox dropdownLanguage;

    public OptionScreen(final MainGame game) {
        this.game = game;

        createUI();
    }

    private void createUI() {
        stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
        skin = new Skin(Gdx.files.internal("ui/uiskin.json"));

        Table table = new Table(skin);
        table.setFillParent(true);
        table.setBounds(0, 0, stage.getWidth(), stage.getHeight());
        table.center();
        table.defaults().pad(6);

        Label labelTitel = new Label("Optionen", skin);
        labelTitel.setFontScale(2);
        table.add(labelTitel).spaceBottom(15).colspan(2).center().row();

        /**
         * Steuerung
         */
        Label label = new Label("Steuerung", skin);
        dropdownControl = new SelectBox(skin);
        dropdownControl.setItems("Manuell", "Automatisch");
        dropdownControl.setSelected(Options.getInstance().getCONTROL());
        table.add(label).center();
        table.add(dropdownControl).fillX().row();

        /**
         * Sprache
         */
        label = new Label("Sprache", skin);
        dropdownLanguage = new SelectBox(skin);
        dropdownLanguage.setItems("Deutsch", "Englisch");
        dropdownLanguage.setSelected(Options.getInstance().getLANGUAGE());
        table.add(label).center();
        table.add(dropdownLanguage).fillX().row();

        /**
         * Debug
         */
        label = new Label("Debug", skin);
        final CheckBox checkBox = new CheckBox("", skin);
        checkBox.setChecked(Options.getInstance().isDEBUG());
        table.add(label).center();
        table.add(checkBox).fill().row();

        /**
         * Buttons
         */
        TextButton button = new TextButton("Speichern", skin);
        button.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Options.getInstance().setCONTROL(dropdownControl.getSelected().toString());
                Options.getInstance().setLANGUAGE(dropdownLanguage.getSelected().toString());
                Options.getInstance().setDEBUG(checkBox.isChecked());

                Options.getInstance().saveOptions();

                //TODO Bestaetigung anzeigen (Toast)

                stage.addAction(Actions.sequence(Actions.moveBy(stage.getWidth(), 0, .5f), Actions.run(new Runnable() {
                            public void run() {
                                if(GameScreen.gameState == GameScreen.GameState.PAUSED)
                                    game.setScreen(game.getLastScreen());
                                else
                                    game.setScreen(new MainMenuScreen(game));
                            }
                        })
                ));
            }
        });
        table.add(button).width(dropdownControl.getPrefWidth()).fillX();

        button = new TextButton("Abbrechen", skin);
        button.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                stage.addAction(Actions.sequence(Actions.moveBy(stage.getWidth(), 0, .5f), Actions.run(new Runnable() {
                            public void run() {
                                if(GameScreen.gameState == GameScreen.GameState.PAUSED)
                                    game.setScreen(game.getLastScreen());
                                else
                                    game.setScreen(new MainMenuScreen(game));
                            }
                        })
                ));
            }
        });
        table.add(button).fillX().row();

        table.debug();
        stage.addActor(table);

        stage.addAction(Actions.sequence(Actions.moveBy(stage.getWidth(), 0), Actions.moveBy(-stage.getWidth(), 0, .5f)));
    }

	@Override
	public void dispose() {
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void render(float arg0) {
        Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        if(Options.getInstance().isDEBUG()) {
            Table.drawDebug(stage);
        }
	}

	@Override
	public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
	}

	@Override
	public void resume() {
	}

	@Override
	public void show() {
        Gdx.input.setInputProcessor(stage);
	}
}
